﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FollowBehaviour : BehaviourNode {

    public override IEnumerator Activate()
    {
        running = true;

        while (this.Target())
        {
            var target = this.Target();
            if (Unit.destination == null || Vector3.Distance(Unit.destination.Value, target.transform.position) > WorldGrid.Instance.gridSize)
            {
                Unit.destination = target.transform.position;
                Unit.path = null;
            }
            if (Vector3.Distance(transform.position, target.transform.position) < Destruct.radius + target.radius)
            {
                Unit.destination = null;
                Unit.path = null;
            }

            yield return StartCoroutine(Test());
            yield return false;
        }

        running = false;
    }
}
